<?php

namespace app\admin\controller;

class News extends Common {
        /*
         * 分类
         */

        public function index() {
                $map = $this->_search("NewsCategory");
                $this->_list(D("NewsCategory"), $map);
                return $this->fetch();
        }

        /*
         * 新闻分类添加
         */

        public function news_cat_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("NewsCategory", true, false);
                        } else {
                                $this->_insert("NewsCategory", true, false);
                        }
                } else {
                        if ($id) {
                                $nc = M("NewsCategory")->field(true)->find($id);
                                $this->assign('nc', $nc);
                        } else {
                                $nc['status'] = 1;
                                $this->assign('nc', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 新闻分类删除
         */

        public function news_cat_del($id = '') {
                $this->_delete("NewsCategory", $id);
        }

        /*
         * 显示/不显示
         */

        public function show_status() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                if ($act == 'do') {
                        $data['status'] = '1';
                        $this->sysAdd("执行了【显示新闻分类】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['status'] = '0';
                        $this->sysAdd("执行了【隐藏新闻分类】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = D("NewsCategory")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 置顶/不置顶
         */

        public function show_istop() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                if ($act == 'do') {
                        $data['istop'] = '1';
                        $this->sysAdd("执行了【置顶新闻】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['istop'] = '0';
                        $this->sysAdd("执行了【取消置顶新闻】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = M("News")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 审核/不审核
         */

        public function show_ischecked() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                if ($act == 'do') {
                        $data['ischecked'] = '1';
                        $data['checkdate'] = NOW_TIME;
                        $this->sysAdd("执行了【审核新闻】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['ischecked'] = '0';
                        $data['checkdate'] = '';
                        $this->sysAdd("执行了【取消审核新闻】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = M("News")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 管理
         */

        public function news() {
                $map = $this->_search("News");
                if (I('post.subject')) {
                        $map['subject'] = array('like', "%" . I('post.subject') . "%");
                }
                $this->_list(D("News"), $map, 'id', false, true);
                $news_cat = M('NewsCategory')->field('id,title')->select();
                $this->assign('news_cat', $news_cat);
                $member = M('Member')->field('id,realname')->select();
                $this->assign('member', $member);
                return $this->fetch();
        }

        /*
         * 新闻删除
         */

        public function news_del($id = '') {
                $this->_delete("News", $id);
        }

        /*
         * 新闻分类添加
         */

        public function news_add($id = '') {
                if (IS_POST) {
                        if ($id) {                               
                                $this->_update("News", false, true);
                        } else {
                                $this->_insert("News", false, true);
                        }
                } else {
                        if ($id) {
                                $nc = M("News")->field(true)->find($id);
                                $this->assign('news', $nc);
                        } else {
                                $nc['cid'] = '';
                                $nc['image_path']='';
                                $this->assign('news', $nc);
                        }
                        $top = M('Location')->where(['pid'=>0])->select();
                        $this->assign('top',$top);
                        $news_cat = M('NewsCategory')->field('id,title')->select();
                        $this->assign('news_cat', $news_cat);
                        return $this->fetch();
                }
        }

}
